const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 系统配置目录
    const configDirId = uuidv4();
    const tableConfigMenuId = uuidv4();

    // 添加系统配置目录菜单
    await queryInterface.bulkInsert('menus', [
      {
        id: configDirId,
        tenant_id: null,
        parent_id: null,
        name: 'config',
        title: '系统配置',
        english: 'System Config',
        type: 'directory',
        path: '/config',
        component: null,
        icon: 'solar:settings-linear',
        permission: null,
        order_num: 50, // 放在较后的位置
        visible: true,
        status: true,
        cache: false,
        redirect: '/config/table',
        created_at: now,
        updated_at: now,
      },
      {
        id: tableConfigMenuId,
        tenant_id: null,
        parent_id: configDirId,
        name: 'tableConfig',
        title: '表格配置',
        english: 'Table Config',
        type: 'menu',
        path: '/config/table',
        component: 'TableConfig/index',
        icon: 'solar:widget-5-linear',
        permission: 'table_config:read',
        order_num: 1,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      }
    ]);

    // 添加表格配置按钮权限
    const tableConfigButtons = [
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tableConfigMenuId,
        name: 'tableConfig:create',
        title: '新增表格配置',
        english: 'Create Table Config',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'table_config:create',
        order_num: 1,
        visible: false,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tableConfigMenuId,
        name: 'tableConfig:update',
        title: '编辑表格配置',
        english: 'Update Table Config',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'table_config:update',
        order_num: 2,
        visible: false,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tableConfigMenuId,
        name: 'tableConfig:delete',
        title: '删除表格配置',
        english: 'Delete Table Config',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'table_config:delete',
        order_num: 3,
        visible: false,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('menus', tableConfigButtons);

    console.log('✅ 系统配置菜单添加成功');
    console.log('   - 系统配置目录');
    console.log('   - 表格配置管理');
    console.log('   - 表格配置按钮权限');
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('menus', {
      name: ['config', 'tableConfig', 'tableConfig:create', 'tableConfig:update', 'tableConfig:delete']
    }, {});
  }
};
